package com.amigo.online.provider.user.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.amigo.online.common.param.dto.user.InvoiceAddressDto;
import com.amigo.online.provider.user.mapper.InvoiceMapper;
import com.amigo.online.provider.user.model.entity.InvoiceAddress;
import com.amigo.online.provider.user.service.InvoiceService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Transactional(rollbackFor = Exception.class)
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceMapper invoiceMapper;


    @Override
    public boolean createInvoiceAddress(InvoiceAddressDto invoiceAddressDto) {

        InvoiceAddress address = new InvoiceAddress();
        BeanUtils.copyProperties(invoiceAddressDto, address);
        address.setCreatedTime(System.currentTimeMillis() / 1000);
        address.setDeleted(0);
        invoiceMapper.insert(address);
        return true;
    }


    @Override
    public boolean updateInvoiceAddress(InvoiceAddressDto invoiceAddressDto) {
        InvoiceAddress address = new InvoiceAddress();
        BeanUtils.copyProperties(invoiceAddressDto, address);
        address.setUpdatedTime(System.currentTimeMillis() / 1000);
        Example example = new Example(InvoiceAddress.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", invoiceAddressDto.getUserId());
        invoiceMapper.updateByExample(address, example);
        return true;
    }


    @Override
    public InvoiceAddress selectAddressByUser(Long userId) {
        InvoiceAddress address = new InvoiceAddress();
        address.setUserId(userId);
        address.setDeleted(0);
        List<InvoiceAddress> addressList = invoiceMapper.select(address);
        return addressList.size() > 0 ? addressList.get(0) : null;

    }


}
